﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseballGame
{
    public class Solution
    {
        public int CalPoints(string[] ops)
        {
            var goals = new Stack<int>();
            foreach (var op in ops)
            {
                switch (op)
                {
                    case "C":
                        goals.Pop();
                        break;
                    case "D":
                        goals.Push(goals.Peek() * 2);
                        break;
                    case "+":
                        var g1 = goals.Pop();
                        var g2 = goals.Pop();
                        var g0 = g1 + g2;
                        goals.Push(g2);
                        goals.Push(g1);
                        goals.Push(g0);
                        break;
                    default:
                        goals.Push(int.Parse(op));
                        break;
                }
            }

            return goals.ToList().Sum();
        }
    }
}
